﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using HIWSystem.Common;
using HIWSystem.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace HIWSystem.ViewModel
{
    public class TestTrackingObsoleteViewModel:ViewModelBase
    {
        #region 属性
  

        private string sn;
        /// <summary>
        /// 单个序列号
        /// </summary>
        public string Sn
        {
            get { return sn; }
            set { sn = value; this.RaisePropertyChanged(() => Sn); }
        }

        private ObservableCollection<string> obsoleteSns;
        /// <summary>
        /// 作废序列号集合
        /// </summary>
        public ObservableCollection<string> ObsoleteSns
        {
            get { return obsoleteSns; }
            set { obsoleteSns = value; this.RaisePropertyChanged(()=>ObsoleteSns); }
        }
        private Brush fontColor;
        /// <summary>
        /// 字体颜色
        /// </summary>
        public Brush FontColor
        {
            get { return fontColor; }
            set { fontColor = value;this.RaisePropertyChanged(()=>FontColor); }
        }
        private string processMsg;
        /// <summary>
        /// 过程信息
        /// </summary>
        public string ProcessMsg
        {
            get { return processMsg; }
            set { processMsg = value; this.RaisePropertyChanged(()=>ProcessMsg); }
        }
        private string selectedReason;
        /// <summary>
        /// 作废的原因
        /// </summary>
        public string SelectedReason
        {
            get { return selectedReason; }
            set { selectedReason = value; this.RaisePropertyChanged(() => SelectedReason); }
        }

        #endregion

        #region 命令
        private RelayCommand obsoleteCommand;
        /// <summary>
        /// 作废命令
        /// </summary>
        public RelayCommand ObsoleteCommand
        {
            get
            {
                if (obsoleteCommand == null)
                {
                    obsoleteCommand = new RelayCommand(Obsolete);
                }
                return obsoleteCommand;
            }
            set { obsoleteCommand = value; }
        }

        private RelayCommand pasteCommand;

        public RelayCommand PasteCommand
        {
            get {
                if (pasteCommand==null)
                {
                    pasteCommand = new RelayCommand(Paste);
                }
                return pasteCommand; }
            set { pasteCommand = value; }
        }
        #endregion

        #region 方法
        private void Paste()
        {
            string[] strLineDatas;
            ObsoleteSns = new ObservableCollection<string>();
            string clipboardText = Clipboard.GetText();
            if (clipboardText!=null || clipboardText!="")
            {
                strLineDatas = clipboardText.Split(new char[] { '\r', '\n' });
                foreach (var line in strLineDatas)
                {
                    if (line!="" && !ObsoleteSns.Contains(line))
                    {
                        ObsoleteSns.Add(line);
                    }
                }
            }
        }
        /// <summary>
        /// 作废
        /// </summary>
        private async void Obsolete()
        {
            if (ObsoleteSns is null || ObsoleteSns.Count()==0)
            {
                MessageBox.Show("请先添加序列号","软件温馨提示",MessageBoxButton.OK,MessageBoxImage.Warning);
                return;
            }
            if (SelectedReason is null)
            {
                MessageBox.Show("请先作废原因", "软件温馨提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            var checkResult = await CheckSn();
            if (!checkResult)
            {
                return;
            }
            await Task.Run(() => {
                foreach (var s in ObsoleteSns)
                {
                    ObsoleteOriginalTestTracking(s);
                }
            });
        }
        //单个序列号作废
        private void ObsoleteOriginalTestTracking(string sn)
        {
            var result=  BusinessProcess.TestTrackingObsolete(sn,SelectedReason,LogInInfo.User);
            if (result.Split('|')[0].Equals("OK")) //success
            {
                Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                {
                    ProcessMsg = string.Format($"序列号【{sn}】作废成功！");
                }));
            }
            else
            {
                Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                {
                    FontColor = Brushes.Black;
                    FontColor = Brushes.Red;
                    ProcessMsg = string.Format($"序列号【{sn}】作废失败！");
                }));
            }
        }
        private async Task<bool> CheckSn()
        {
            var result = true;
            var lstSns = ObsoleteSns.ToList();
            var dicSnResult = new List<string>();
            await Task.Run(()=> {
                var cResult = false;//检查的结果
                foreach (var sn in lstSns)
                {
                   cResult=BusinessProcess.SNIsExists(sn);
                   dicSnResult.Add(string.Format($"{cResult}|{sn}"));
                }
            });
            var index = 1;
            foreach (var sr in dicSnResult)
            {
                var r=string.Empty;
                var sn = string.Empty;
                r = sr.Split('|')[0];
                sn = sr.Split('|')[1];
                if (r.ToUpper() == "TRUE")
                {
                    FontColor = Brushes.Black;
                    FontColor = Brushes.Green;
                }
                else
                {
                    FontColor = Brushes.Black;
                    FontColor = Brushes.Red;
                    result = false;
                }
                ProcessMsg = string.Format("第【{0}】序列号【{1}】是【{2}】",index,sn,r.ToUpper()=="TRUE"?"存在":"不存在");
                index++;
            }
            return result;
        }
        #endregion
    }
}
